package com.ocean.recommendationserver.realtimerecommend.crawl;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.HashMap;
import java.util.Map;

public class BaseAuthenticator extends Authenticator {
	private Map<String, PasswordAuthentication> authMap = new HashMap<String, PasswordAuthentication>();

	public synchronized void add(String host, int port, String userName, String password) {
		if (authMap.size() > 1000) {
			authMap.clear();
		}
		if (userName == null) {
			return;
		}
		authMap.put(host + ":" + port, new PasswordAuthentication(userName, password.toCharArray()));

	}

	protected PasswordAuthentication getPasswordAuthentication() {
		PasswordAuthentication authentication = authMap.get(getRequestingHost() + ":" + getRequestingPort());
		return authentication;
	}
}
